package com.isoft.util;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class TimeUtil {
    public static String getDatePoor(String dateString,String dateString2)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

        Date date1 = null;
        Date date2  =null;
        try {
            date1 = format.parse(dateString);
            date2 = format.parse(dateString2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        // 设置 Calendar 对象的时间为给定的日期
        cal1.setTime(date1);
        cal2.setTime(date2);

        // 获取两个日期之间相差的毫秒数
        long diffInMillis = Math.abs(cal2.getTimeInMillis() - cal1.getTimeInMillis());

        // 计算相差的小时数
        long diffInHours = diffInMillis / (60 * 60 * 1000) % 24;

        // 计算相差的分钟数
        long diffInMinutes = diffInMillis / (60 * 1000) % 60;

        // 计算相差的秒数
        long diffInSeconds = diffInMillis / 1000 % 60;

        return  diffInHours+"小时"+diffInMinutes+"分";
    }

    public static String getDateEnd(String dateString,String dateString2,String dateString3)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

        Date date1 = null;
        Date date2  =null;
        Date date3=null;
        try {
            date1 = format.parse(dateString);
            date2 = format.parse(dateString2);
            date3=format.parse(dateString3);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        Calendar cal3 = Calendar.getInstance();

        // 设置 Calendar 对象的时间为给定的日期
        cal1.setTime(date1);
        cal2.setTime(date2);
        cal3.setTime(date3);

        // 获取两个日期之间相差的毫秒数
        long diffInMillis = Math.abs(cal2.getTimeInMillis() - cal1.getTimeInMillis());
        long endTime=cal3.getTimeInMillis()+diffInMillis;
        Date date=new Date(endTime);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format1 = simpleDateFormat.format(date);
        return  format1+"";
    }
}
